package com.yyou.chat_gpt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * @ClassName QueryUsage
 * @Author ZengRJ
 * @Date 2023/7/30 15:19
 * @Version 1.0
 */
public class ChatGPTQuotaQuery {
    public static void main(String[] args) {
        try {
            // 设置API密钥
            String apiKey = "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";

            // 构建查询URL
            String apiUrl = "https://112.213.125.247/v1/usage/chat/completions";
            URL url = new URL(apiUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Authorization", "Bearer " + apiKey);

            // 发送请求并获取响应
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 解析响应并打印额度信息
                System.out.println(response.toString());
            } else {
                System.out.println("查询失败，错误码：" + responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}